(function(mui,document){
	mui.init({
		swipeBack:true, //启用右滑关闭功能
	});
	
	mui.plusReady(function() {
		app.ajax("joinProductIndex",null,function(data){
			initIndex(data);
		});
		var searchBtn = document.getElementById('search');
		var searchInput = document.getElementById("searchInput");
		searchInput.addEventListener("keydown",function(e){
	        if(13 == e.keyCode){
	            mui.trigger(searchBtn,'tap');
	            searchInput.blur();
	        }
	    },false);
		searchBtn.addEventListener('tap', function(event) {
			if(!searchInput.value){
				plus.nativeUI.toast();
				return;
			}
			searchInput.blur();
			app.ajax("joinProductSearch",{searchData:searchInput.value},function(data){
				initIndex(data);
			});
		});
	});
	
	function initIndex(data){
		var searchResult = document.getElementById("search_result");
		searchResult.innerHTML = "";
		var ul = document.createElement("ul");
		ul.setAttribute("class","mui-table-view");
		ul.setAttribute("id","dataArea");
		searchResult.appendChild(ul);
		if(data.length == 0){
			var li = document.createElement("li");
				li.setAttribute("class","mui-table-view-cell");
			var h5_1 = document.createElement("h5");
				h5_1.innerHTML="未找到结果，请重试！";
			li.appendChild(h5_1);
			ul.appendChild(li);
			return;
		}
		mui.each(data,function(i,item){
			// 往页面中添加项目基本信息
			var alink = initProject(item,searchResult,ul);
			alink.setAttribute("show",true);
			// 为链接绑定点击函数
			alink.addEventListener("tap",function(event){
				if(alink.getAttribute("show") == "true"){
					app.ajax("joinProductContent",{"deptId":item.id},function(content){
						var h5_content = createEle("h5","member",content);
						h5_content.setAttribute("style","line-height: 18px;");
						h5_content.setAttribute("id","hide_" + item.id);
						alink = append(alink,h5_content);
					});
					alink.setAttribute("show","false");
				} else {
					var h5 = document.getElementById("hide_" + item.id);
					alink.removeChild(h5);
					alink.setAttribute("show","true");
				}
			});
		});
	}
	
	function initProject(item,searchResult,ul){
		var li = createEle("li","mui-table-view-cell");
		var h5_1 = createEle("h4","mui-ellipsis projectTitle",item.productName);
		var h5_2 = createEle("h5","projectCharger",item.schoolName);
		var h5_3 = createEle("h5","projectCharger",item.schoolCharger);
		if(item.schoolChargerPhone){
			h5_3.innerHTML = h5_3.innerHTML + ":" + item.schoolChargerPhone;
		}
		var deptName = createEle("h5","",item.deptName);
		var h5_4 = createEle("h5","",item.deptLinkMan);
		if(item.deptLinkPhone){
			h5_4.innerHTML = h5_4.innerHTML + ":" + item.deptLinkPhone;
		}
		var subject = createEle("h5","",item.subject);
		li = append(li,h5_1,h5_2,h5_3,deptName,h5_4,subject); 
		ul.appendChild(li);
		return li;
	}
	
})(mui,document);